package com.wswy.wzcx.module

import android.support.design.widget.TabLayout
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import android.support.v4.view.ViewPager
import android.view.ViewGroup
import com.wswy.wzcx.AppContext
import com.wswy.wzcx.model.news.NewsCategory
import com.wswy.wzcx.module.base.Resource
import com.wswy.wzcx.statistics.StatTools
import com.wswy.wzcx.statistics.StatisticsId
import com.wswy.wzcx.ui.main.home.HomeNewsFragment
import com.wswy.wzcx.ui.main.home.INewsView
import com.wswy.wzcx.utils.FragUtils
import com.wswy.wzcx.widget.UnderlinePageIndicator

class NewsController (

        private val tabLayout:TabLayout,
        private val indicator : UnderlinePageIndicator,
        private val vp_news: ViewPager,


        private val currFrgRv:((INewsView)->Unit?)?=null
) {

    fun load(fm: FragmentManager){
        HomeNewsCategoryLoader().loader {
            showNewsCategory(it.orNull(),fm)
        }
    }

    private fun showNewsCategory(data: List<NewsCategory>?,fm: FragmentManager) {
        if (data == null || data.isEmpty()) {
            return
        }

        val list = ArrayList<NewsCategory>(5)

        if (data.size < 5) {
            list.addAll(data)
        } else {
            list.addAll(data.subList(0, 5))
        }

        if (tabLayout.childCount > 0){
            tabLayout.removeAllTabs()
            FragUtils.removeAll(fm)
        }

        val arrays = arrayOfNulls<String>(list.size)
        var i = 0
        for (newsCategory in list) {
            arrays[i++] = newsCategory.name
        }

        for (array in arrays) {
            tabLayout.addTab(tabLayout.newTab().setText(array))
        }

        tabLayout.setupWithViewPager(vp_news)
        vp_news.offscreenPageLimit = 5

        vp_news.clearOnPageChangeListeners()

        vp_news.adapter = object : FragmentPagerAdapter(fm) {

            override fun setPrimaryItem(container: ViewGroup, position: Int, frg: Any) {
                super.setPrimaryItem(container, position, frg)

                (frg as? INewsView)?.run {
                    currFrgRv?.invoke(this)
                }

            }

            override fun getCount(): Int {
                return arrays.size
            }

            override fun getItem(position: Int): Fragment {
                return HomeNewsFragment.newInstance(list[position],0)
            }

            override fun getPageTitle(position: Int): CharSequence? {
                return arrays[position]
            }

        }

        vp_news.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {

                StatTools.sendShow(AppContext.getContext(), StatisticsId.click_News_Type, list[position].type)
            }

            override fun onPageScrollStateChanged(state: Int) {

            }
        })

        indicator.setTabLayoutAndViewPager(tabLayout, vp_news)
        tabLayout.requestLayout()
    }


}